package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysSensitive;
import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/sensitive")
public class SysSensitiveController {
    private static final Log log = Logs.get();
    @Inject
    @Reference
    private SysSensitiveService sensitiveService;

    @At("")
    @Ok("beetl:/platform/sys/sensitive/index.html")
    @RequiresPermissions("sys.manager.sensitive")
    public void index() {

    }

    /**
     * 查询关键词列表（分页）
     *
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate    String[] 时间数组
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.sensitive")
    public Object data(@Param("searchKeyword") String searchKeyword, @Param("searchDate") String[] searchDate, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        return Result.success().addData(sensitiveService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }

    /**
     * 添加敏感词
     * @param sysSensitive SysSensitive
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("sys.manager.sensitive.add")
    public Object add(@Param("..") SysSensitive sysSensitive) {
        try {
            sensitiveService.insert(sysSensitive);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /**
     * 查询敏感词
     * @param id         String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        try {
            return Result.success(sensitiveService.fetch(id));
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 修改敏感词
     * @param SysSensitive SysSensitive
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("sys.manager.sensitive.edit")
    public Object editDo(@Param("..") SysSensitive SysSensitive) {
        try {
            sensitiveService.update(SysSensitive);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }


    /*
     * 删除敏感词
     * @param id String
     * @return Object
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.manager.sensitive.delete")
    public Object delete(String id) {
        try {
            sensitiveService.delete(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }

    }
}